package com.ttscm.srds.web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ttscm.srds.datagram.SeerDataPacket;
import com.ttscm.srds.pojo.ResponseResult;
import com.ttscm.srds.robot.seer.RobotStatus;
import com.ttscm.srds.robot.seer.RobotTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tinylog.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/bd")
public final class BdController {

    @Autowired
    private RobotStatus robotStatus;

    @Autowired
    private RobotTask robotTask;

    @GetMapping("/location")
    public ResponseResult location() throws IOException {
        Logger.info("/bd/location");

        ObjectMapper objectMapper = new ObjectMapper();
        SeerDataPacket dataPacket1004 = robotStatus.location1004();
        Map<String, Object> resultLocation1004 = objectMapper.readValue(dataPacket1004.getBodyDataArea(), new TypeReference<Map<String, Object>>() {
        });
        int resultCode = (int) resultLocation1004.get("ret_code");
        if (resultCode != 0) {
            return ResponseResult.fail(resultCode, (String) resultLocation1004.get("err_msg"));
        }
        String currentStation = (String) resultLocation1004.get("current_station");

        return ResponseResult.succeed(currentStation);
    }

    @GetMapping("/taskStatus")
    public ResponseResult taskStatus() throws IOException {
        Logger.info("/bd/taskStatus");

        SeerDataPacket dataPacket1020 = robotStatus.taskStatus1020();
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> resultTaskStatus1020 = objectMapper.readValue(dataPacket1020.getBodyDataArea(), new TypeReference<Map<String, Object>>() {
        });
        int resultCode = (int) resultTaskStatus1020.get("ret_code");
        if (resultCode != 0) {
            return ResponseResult.fail(resultCode, String.valueOf(resultTaskStatus1020.get("err_msg")));
        }

        int taskStatus1 = (int) resultTaskStatus1020.get("task_status");
        String statusDesc = "无任务...";
        if (taskStatus1 == 2) {
            statusDesc = "任务执行中...";
        } else if (taskStatus1 == 3) {
            statusDesc = "任务挂起中...";
        } else if (taskStatus1 == 4) {
            statusDesc = "任务已完成...";
        } else if (taskStatus1 == 5) {
            statusDesc = "任务已失败...";
        } else if (taskStatus1 == 6) {
            statusDesc = "任务已取消...";
        }
        Map<String, Object> taskStatus = new HashMap<>();
        taskStatus.put("statusCode", taskStatus1);
        taskStatus.put("statusDesc", statusDesc);
        taskStatus.put("targetId", resultTaskStatus1020.get("target_id"));

        return ResponseResult.succeed(taskStatus);
    }

    @GetMapping("/execTask/{taskListName}")
    public ResponseResult execTask(@PathVariable("taskListName") String taskListName) throws IOException {
        Logger.info("/bd/execTask");

        ObjectMapper objectMapper = new ObjectMapper();
        // 执行任务链
        SeerDataPacket dataPacket3106 = robotTask.taskListName3106(taskListName);
        Map<String, Object> result30513106 = objectMapper.readValue(dataPacket3106.getBodyDataArea(), new TypeReference<Map<String, Object>>() {
        });
        int resultCode3106 = (int) result30513106.get("ret_code");
        if (resultCode3106 != 0) {
            return ResponseResult.fail(resultCode3106, String.valueOf(result30513106.get("err_msg")));
        }
        return ResponseResult.succeed();
    }

}
